package com.uchiha.nova.core.error;

import com.uchiha.nova.core.publisher.ErrorLogPublisher;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.api.ResultCode;
import com.uchiha.nova.core.tool.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.lang.Nullable;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @author 周林静
 * @Date 2021-10-26 8:47
 * @Version 1.0
 */
@Slf4j
public class NovaErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        String requestUri = this.getAttr(webRequest, "javax.servlet.error.request_uri");
        Integer status = this.getAttr(webRequest, "javax.servlet.error.status_code");
        Throwable error = getError(webRequest);
        R result;
        if (error == null) {
            log.error("URL:{} error status:{}", requestUri, status);
            result = R.fail(ResultCode.FAILURE, "系统未知异常[HttpStatus]:" + status);
        } else {
            log.error(String.format("URL:%s error status:%d", requestUri, status), error);
            result = R.fail(status, error.getMessage());
        }
        //发送服务异常事件
        ErrorLogPublisher.publishEvent(error, requestUri);
        return BeanUtil.toMap(result);
    }

    @Nullable
    private <T> T getAttr(WebRequest webRequest, String name) {
        return (T) webRequest.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }


}
